/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "RasterizerState.h"

#include <D3D11.h>
#include <tinyxml.h>
#include "StringToEnum.h"

#include "../Managers/DX11Manager.h"

namespace Hogshead
{
	namespace DX11Rendering
	{
		ContentCPP(Hogshead::DX11Rendering::RasterizerState, RasterizerState)

		

		RasterizerState::RasterizerState(void)
		{
			

			_rasterizer_state = NULL;
		}


		RasterizerState::~RasterizerState(void)
		{
		}

		void RasterizerState::finalize()
		{
			if(_rasterizer_state)
				_rasterizer_state->Release();
		}

		void RasterizerState::explicitLoad()
		{
			throw Exception("You cannot load a RasterizerState in this manor, XML is required");
		}

		void RasterizerState::readXML(const String& in_xml)
		{
			//parse the String for what we need
			TiXmlDocument document;
			document.Parse(in_xml);

			TiXmlElement* root_element = document.RootElement();
			String root_tag(root_element->Value());

			if(root_tag.equals("Content"))
			{
				//get the standard stuff
				this->_key = String(root_element->Attribute("key"));
				this->_path = String(root_element->Attribute("path"));

				//used to get the absolute path to it 
				this->_path = ContentLibrary::getCompleteContentString( this->_path );


				int depth_bias;
				float depth_bias_clamp, slope_scaled_depth_bias;
				bool front_counter_clockwise, depth_clip_enable, scissor_enable, multisample_enable, antialiased_line_enable;

				D3D11_RASTERIZER_DESC rd;

				
				const char* fill_mode = root_element->Attribute("FillMode");
				if(fill_mode != NULL)
				{
					rd.FillMode = (*StringToEnum::_fill_mode_dic)[fill_mode];
				}
				else
				{
					rd.FillMode = D3D11_FILL_SOLID;
				}

				const char* cull_mode = root_element->Attribute("CullMode");
				if(cull_mode != NULL)
				{
					rd.CullMode = (*StringToEnum::_cull_mode_dic)[cull_mode];
				}
				else
				{
					rd.CullMode = D3D11_CULL_BACK;
				}

				int result;
				result = root_element->QueryBoolAttribute("FrontCounterClockwise", &front_counter_clockwise);
				if(result != TIXML_NO_ATTRIBUTE)
				{
					rd.FrontCounterClockwise = front_counter_clockwise;
				}
				else
				{
					rd.FrontCounterClockwise = true;
				}

				result = root_element->QueryIntAttribute("DepthBias", &depth_bias);
				if(result != TIXML_NO_ATTRIBUTE)
				{
					rd.DepthBias = depth_bias;
				}
				else
				{
					rd.DepthBias = 0;
				}

				result = root_element->QueryFloatAttribute("DepthBiasClamp", &depth_bias_clamp);
				if(result != TIXML_NO_ATTRIBUTE)
				{
					rd.DepthBiasClamp = depth_bias_clamp;
				}
				else
				{
					rd.DepthBiasClamp = 0.0f;
				}

				result = root_element->QueryFloatAttribute("SlopeScaledDepthBias", &slope_scaled_depth_bias);
				if(result != TIXML_NO_ATTRIBUTE)
				{
					rd.SlopeScaledDepthBias = slope_scaled_depth_bias;
				}
				else
				{
					rd.SlopeScaledDepthBias = 0.0f;
				}

				result = root_element->QueryBoolAttribute("DepthClipEnable", &depth_clip_enable);
				if(result != TIXML_NO_ATTRIBUTE)
				{
					rd.DepthClipEnable = depth_clip_enable;
				}
				else
				{
					rd.DepthClipEnable = true;
				}

				result = root_element->QueryBoolAttribute("ScissorEnable", &scissor_enable);
				if(result != TIXML_NO_ATTRIBUTE)
				{
					rd.ScissorEnable = scissor_enable;
				}
				else
				{
					rd.ScissorEnable = false;
				}

				result = root_element->QueryBoolAttribute("MultisampleEnable", &multisample_enable);
				if(result != TIXML_NO_ATTRIBUTE)
				{
					rd.MultisampleEnable = multisample_enable;
				}
				else
				{
					rd.MultisampleEnable = false;
				}

				result = root_element->QueryBoolAttribute("AntialiasedLineEnable", &antialiased_line_enable);
				if(result != TIXML_NO_ATTRIBUTE)
				{
					rd.AntialiasedLineEnable = antialiased_line_enable;
				}
				else
				{
					rd.AntialiasedLineEnable = false;
				}


				
				HRESULT hr = DX11Manager::createRasterizerState(&rd, &_rasterizer_state);
				if(FAILED(hr))
				{
					StringBuilder sb;
					sb << "ERROR ON CONTENT LOAD: Rasterizer State could not be created:" << _path;
					DX11Manager::_logger->write(sb);
					throw new Exception(sb);
				}
#ifdef _DEBUG
				hr = _rasterizer_state->SetPrivateData( WKPDID_D3DDebugObjectName, _path.length() - 1, _path );
				if(FAILED(hr))
				{
					StringBuilder sb;
					sb << "ERROR ON LOAD: Rasterizer State could not be named: " << _path;
					DX11Manager::_logger->write(sb);
					throw new Exception(sb);
				}
#endif
			}

			return;
		}
	}
}
